<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="所属公司" prop="companyId">
        <el-tree-select
          v-model="queryParams.companyId"
          :data="companyList"
          :render-after-expand="true"
          :props="treeProps"
          node-key="id"
          clearable check-strictly
          @change="companyChange"
        />
      </el-form-item>
      <el-form-item label="所属部门" prop="departId">
        <el-select v-model="queryParams.departId" placeholder="请选择" clearable>
          <el-option
            v-for="item in departmentList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资产名称" prop="assetName">
        <el-input
          v-model="queryParams.assetName"
          placeholder="请输入资产名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产编码" prop="assetNumber">
        <el-input
          v-model="queryParams.assetNumber"
          placeholder="请输入资产编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产类型" prop="assetType">
        <el-select v-model="queryParams.assetType" placeholder="请选择" clearable>
          <el-option
            v-for="item in fixed_assets_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资产状态" prop="assetStatus">
        <el-select v-model="queryParams.assetStatus" placeholder="请选择" clearable>
          <el-option
            v-for="item in fixed_assets_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="启用日期" prop="enablingTime">
        <el-date-picker clearable
                        v-model="queryParams.enablingTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择启用日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['plat:assets:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="Upload"
            @click="handleImport"
            v-hasPermi="['plat:assets:import']"
          >导入</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['plat:assets:export']"
          >导出</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Printer"
            :disabled="multiple"
            @click="printQrCode"
            v-hasPermi="['plat:assets:print']"
          >二维码打印</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table class="fixed-scrollbar" v-horizontal-scroll border v-loading="loading" :data="assetsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="资产名称" align="center" prop="assetName" width="120" show-overflow-tooltip />
        <el-table-column label="资产编码" align="center" prop="assetNumber" min-width="150" show-overflow-tooltip />
        <el-table-column label="资产类型" align="center" prop="assetType" width="100">
          <template #default="scope">
            <dict-tag :options="fixed_assets_type" :value="scope.row.assetType" />
          </template>
        </el-table-column>
        <el-table-column label="规格型号" align="center" prop="assetModel" width="120" show-overflow-tooltip />
        <el-table-column label="所属公司" align="center" prop="companyName" width="120" show-overflow-tooltip />
        <el-table-column label="所属部门" align="center" prop="departName" width="120" show-overflow-tooltip />
        <el-table-column label="负责人" align="center" prop="personLiable" width="120" show-overflow-tooltip />
        <el-table-column label="资产状态" align="center" prop="assetStatus" width="100">
          <template #default="scope">
            <dict-tag :options="fixed_assets_status" :value="scope.row.assetStatus" />
          </template>
        </el-table-column>
        <el-table-column label="存放地点" align="center" prop="location" min-width="150" show-overflow-tooltip />
        <el-table-column label="启用日期" align="center" prop="enablingTime" width="150">
          <template #default="scope">
            <span>{{ parseTime(scope.row.enablingTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="资产备注" align="center" prop="remark" width="100" show-overflow-tooltip />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100" fixed="right">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['plat:assets:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['plat:assets:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改固定资产对话框 -->
    <Dialog v-model="showDialog" :title="dialogTitle" width="60%">
      <edit-assets 
        v-if="dialogType == 1"
        ref="editAssets"
        :company-list="companyList"
        @cancel="cancel"
        @confirm="confirm"
      />
      <upload-assets 
        v-if="dialogType == 2"
        @cancel="cancel"
        @confirm="confirm"
      />
      <print-assets 
        v-if="dialogType == 3"
        :list="selectedItems"
        @cancel="cancel"
      />
    </Dialog>
  </div>
</template>

<script setup name="Assets">
import { listAssets, delAssets } from "@/api/asset/list"
import { getAllCompanies } from '@/api/company/company'
import { listDepartment } from "@/api/company/department"
import { nextTick } from "vue"
import EditAssets from './edit.vue'
import UploadAssets from './upload.vue'
import PrintAssets from './print.vue'

const { proxy } = getCurrentInstance()
const { fixed_assets_type,fixed_assets_status } = proxy.useDict("fixed_assets_type","fixed_assets_status")

const editAssets = ref()
const assetsList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const showDialog = ref(false)
const dialogTitle = ref('')
const dialogType = ref()          // 1新增修改  2导入  3打印
const departmentList = ref([])
const companyList = ref([])
const treeProps = {
  label: 'name',
  disabled: (data, node) => {
    return data.check == false
  }
}
const selectedItems = ref([])

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    companyId: null,
    departId: null,
    assetName: null,
    assetNumber: null,
    assetType: null,
    assetStatus: null,
    enablingTime: null,
  }
})

const { queryParams } = toRefs(data)

function companyChange() {
  queryParams.value.department = null
  getDepartmentList()
}

/** 公司列表 */
function getCompanyList() {
  getAllCompanies().then(res => {
    companyList.value = res.data || []
  })
}

/** 部门列表 */
function getDepartmentList() {
  const params = {
    pageNum: 1,
    pageSize: 100,
    companyId: queryParams.value.companyId
  }
  listDepartment(params).then(response => {
    departmentList.value = response.rows
  })
}

/** 查询固定资产列表 */
function getList() {
  loading.value = true
  listAssets(queryParams.value).then(response => {
    assetsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  showDialog.value = false
  dialogTitle.value = ''
}

function confirm() {
  cancel()
  getList()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  selectedItems.value = selection
  ids.value = selection.map(item => item.assetNumber)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  showDialog.value = true
  dialogType.value = 1
  dialogTitle.value = '添加资产'
  nextTick(() => {
    editAssets.value.pageInit(1)
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _assetNumber = row.assetNumber || ids.value
  showDialog.value = true
  dialogType.value = 1
  dialogTitle.value = '修改资产'
  nextTick(() => {
    editAssets.value.pageInit(2, _assetNumber)
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _assetNumbers = row.assetNumber || ids.value
  proxy.$modal.confirm('是否确认删除该资产？').then(function() {
    return delAssets(_assetNumbers)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

// 导入
function handleImport() {
  showDialog.value = true
  dialogType.value = 2
  dialogTitle.value = '资产导入'
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('plat/assets/export', {
    ...queryParams.value
  }, `资产列表_${proxy.getFileImportName()}.xlsx`)
}

/** 二维码打印 */
function printQrCode() {
  showDialog.value = true
  dialogType.value = 3
  dialogTitle.value = '二维码打印'
}

getCompanyList()
getList()
</script>
